SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[sp_StatisticFinishedProducts]
	@fromDate		DATE,
	@toDate			DATE
AS
BEGIN
	
	SET NOCOUNT ON;

    WITH TAB1 AS
	(
		SELECT [code], [customerId], [materialId], [description], [orderQuantity]
		FROM [Order],[OrderMaterial]
		WHERE [Order].[isFinished]=1
		AND [Order].[id] = [OrderMaterial].[orderId]
		AND [deliveryDate] IS NOT NULL
		AND	[deliveryDate] <= @toDate
		AND	@fromDate <= [deliveryDate]
	)
	SELECT [code], [customerId], [materialId], [description], [length] + ' x ' + [width] AS [size], [orderQuantity]
	FROM TAB1, [Size]
END
GO
